// pages/login/login.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      imgPath:app.globalData.imgPath,
      canIUse: wx.canIUse('button.open-type.getUserInfo')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindGetUserInfo:function(e){
    let code, rawData, iv, signature, encryptedData, referPhone ;
    if (e.detail.userInfo) {//用户按了允许授权按钮   
          wx.login({
            success(res) {
              if (res.code) {
                console.log(res)

                code=res.code

                wx.getUserInfo({
                  success(res) {
                    rawData = res.rawData;
                    iv = res.iv;
                    signature = res.signature;
                    encryptedData = res.encryptedData;
                    referPhone = wx.getStorageSync('referPhone');
                    console.log(referPhone)
                    let data = {}
                  if (referPhone) {
                      data = {
                        code: code,
                        rawData: res.rawData,
                        iv: res.iv,
                        signature: res.signature,
                        encryptedData: res.encryptedData,
                        referPhone: referPhone
                      }
                    } else {
                      data = {
                        code: code,
                        rawData: res.rawData,
                        iv: res.iv,
                        signature: res.signature,
                        encryptedData: res.encryptedData
                      }
                    }
                    console.log(data)
                    wx.request({
                      url: app.globalData.reqPath + 'shop/MiniProgramLogin',
                      data: data,
                      method: 'POST',
                      success: function ({ data, header }) {
                        let authorization = header.authorization || header.Authorization;
                        app.globalData.authorization = authorization;
                        wx.setStorage({
                          key: 'authorization',
                          data: authorization,
                        })
                        if (data.code == 200) {
                          console.log("login:" + data.data.bindPhone)
                          wx.setStorageSync('isLogin', true)
                          if (data.data.bindPhone == 0) {
                            wx.redirectTo({
                              url: '/pages/bindTel/bindTel',
                            })
                          } else {
                            //获取个人信息
                            wx.request({
                              url: app.globalData.reqPath + 'shop/Members',
                              data: {},
                              header: {
                                'authorization': app.globalData.authorization
                              },
                              success: function ({ data, header }) {
                                if (data.code == 200) {
                                  wx.setStorage({
                                    key: 'userInfo',
                                    data: data.data,
                                  })
                                }
                              }
                            })
                            wx.switchTab({
                              url: '/pages/index/index',
                            })

                          }
                        }
                      }
                    })
                  }
                })
            }
            }
          })
         
            
        
      }
  }
  // login:function(e){//登录
  //  let params=e.detail.value;
  //     wx.request({
  //       url:app.globalData.reqPath+'shop/Authorization',
  //       method:'POST',
  //       data:{
  //         phone:params.tel,
  //         password: params.paw
  //       },
  //       header:{
  //         'authorization': app.globalData.authorization
  //       },
  //       success:function({data,header}){
  //         if(data.code==200){
  //           app.globalData.authorization=header.authorization;
  //           wx.setStorageSync('authorization', header.authorization);
  //           wx.showToast({
  //             title: '登录成功',
  //             icon:'none'
  //           })
  //           setTimeout(function(){
  //             wx.switchTab({
  //               url: '/pages/index/index',
  //             })
  //           },300)
          
  //         }else{
  //           wx.showToast({
  //             title: data.data.msg,
  //             icon:'none'
  //           })
  //         }
  //       }
  //     })
  // }
})